package com.example.demo15.controller;

import com.example.demo15.entity.User;
import com.example.demo15.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import jakarta.servlet.http.HttpSession;

@Controller
@Slf4j
public class AuthController {

    @Autowired
    private UserService userService;

    /**
     * 显示登录页面
     * 处理GET请求：/login
     */
    @GetMapping("/login")
    public String showLogin(@RequestParam(required = false) String msg, Model model) {
        if (msg != null) {
            model.addAttribute("msg", msg); // 将提示信息传递给视图
        }
        return "login"; // 返回登录页面模板名称
    }

    /**
     * 显示注册页面
     * 处理GET请求：/register
     */
    @GetMapping("/register")
    public String showRegister() {
        return "register";
    }

    /**
     * 处理用户注册请求
     */
    @PostMapping("/register")
    public String registerUser(@RequestParam String username,
                               @RequestParam String password,
                               Model model) {
        if (userService.checkIfUserExists(username)) {
            model.addAttribute("msg", "用户名已存在");
            return "register";
        }

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        userService.save(user);

        return "redirect:/login";
    }

    /**
     * 处理用户登录请求
     */
    @PostMapping("/login-process")
    public String loginUser(@RequestParam String username,
                            @RequestParam String password,
                            Model model,
                            HttpSession session) {
        return userService.findByUsername(username)
                .filter(user -> user.getPassword().equals(password))
                .map(user -> {
                    session.setAttribute("user", user);
                    return "redirect:/dashboard"; // 应该跳转到这里
                })
                .orElseGet(() -> {
                    model.addAttribute("msg", "用户名或密码错误");
                    return "login";
                });
    }

    // 在 AuthController.java 中添加以下代码

    /**
     * 显示用户信息修改页面
     * 处理GET请求：/profile
     */
    @GetMapping("/profile")
    public String showProfile(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        model.addAttribute("user", user);
        return "profile";
    }

    /**
     * 处理用户信息修改请求
     * 处理POST请求：/profile
     */
    @PostMapping("/profile")
    public String updateProfile(@RequestParam String username, @RequestParam(required = false) String password,
                                @RequestParam(required = false) String confirmPassword, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }

        // 如果密码不为空且两次输入的密码一致，则更新密码
        if (password != null && !password.isEmpty() && password.equals(confirmPassword)) {
            user.setPassword(password);
        }

        user.setUsername(username);
        userService.save(user);
        session.setAttribute("user", user);

        return "redirect:/dashboard";
    }



}
